<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

// +----------------------------------------------------------------------
// | 公共 验证权限
// +----------------------------------------------------------------------
namespace app\controller;
use think\facade\View;
use think\facade\Db;

class Common extends Base
{
	public $aid;
	public $uid;
	public $user;
    public $admin;
	public $mdid = 0;
	public $auth_data = 'all';
	public $platform = 'mp';
	public $xcxaid = 0;
    public function initialize(){
		parent::initialize();
		$request = request();
//        $file = ROOT_PATH.'runtime/log/aaa.txt';
//        file_put_contents($file,$_POST['apifrom']);
        //如果是vueapi的请求，则不执行header跳转，直接返回状态给接口前端
		if(input('param.apifrom')=='vue'){
            //测试
            //测试 end
            if(!session("?ADMIN_LOGIN")){
                echojson(['status'=>-1,'msg'=>'请重新登录']);die();
            }
        }else{
            if(!in_array($request->controller(),array('login')) && !session("?ADMIN_LOGIN")){
                header('Location:'.(string)url('login/index'));die;
            }
        }
		$this->aid = session('ADMIN_AID');
		if(MN == 'business'){
			$this->uid = session('ADMIN_AUTH_UID');
			$this->bid = session('ADMIN_AUTH_BID');
		}else{
			$this->uid = session('ADMIN_UID');
			$this->bid = session('ADMIN_BID');
		}
		define('aid',$this->aid);
		define('bid',$this->bid);
		define('uid',$this->uid);
		$user = Db::name('admin_user')->where('id',$this->uid)->find();
		if($user['groupid']){
			$group = Db::name('admin_user_group')->where('id',$user['groupid'])->find();
			$user['auth_data'] = $group['auth_data'];
			$user['wxauth_data'] = $group['wxauth_data'];
			$user['notice_auth_data'] = $group['notice_auth_data'];
			$user['hexiao_auth_data'] = $group['hexiao_auth_data'];
			$user['mdid'] = $group['mdid'];
			$user['showtj'] = $group['showtj'];
		}
		if($user['bid'] > 0 && $user['auth_type'] == 1){
			$adminuser = Db::name('admin_user')->where('aid',$this->aid)->where('isadmin','>',0)->find();
			$user['auth_type'] = $adminuser['auth_type'];
			$user['auth_data'] = $adminuser['auth_data'];
		}
		$this->user = $user;
		$this->mdid = $user['mdid'];
		if($user['auth_type']==0){
			$auth_data = json_decode($user['auth_data'],true);
			$auth_path = \app\common\Menu::blacklist();
			foreach($auth_data as $v){
				$auth_path = array_merge($auth_path,explode(',',$v));
			}

			$thispath = $request->controller() .'/'.$request->action();
			if(!in_array($request->controller().'/*',$auth_path) && !in_array($thispath,$auth_path) && !session('BST_ID')){
                die('无访问权限');
			}
			$this->auth_data = $auth_path;
		}else{
			$this->auth_data = 'all';
		}
		View::assign('aid',$this->aid);
		View::assign('bid',$this->bid);
		View::assign('auth_data',$this->auth_data);

		$platform = \app\common\Common::getplatform(aid);
		$this->platform = $platform;
		View::assign('platform',$platform);

		$admin = Db::name('admin')->where('id',aid)->find();
        $this->admin = $admin;
		if($admin['domain']){
			define('PRE_URL2','https://'.$admin['domain']);
		}else{
			define('PRE_URL2',PRE_URL);
		}
    }

    /**
     * @param array $titleArr
     * @param array $dataArr
     * @param array $typeArr 类型数组
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
	function export_excel(array $titleArr,array $dataArr,array $typeArr=[]){
		$phpexcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();//实例化
		$phpsheet = $phpexcel->getActiveSheet();
		//填充表头信息
		$widthArr = [];
		for($i = 0;$i < count($titleArr);$i++) {
			$phpsheet->setCellValue(IntToChr($i)."1","$titleArr[$i]");
			$widthArr[] = strlen($titleArr[$i]);
		}
		//填充表格信息
		for ($i = 2;$i <= count($dataArr) + 1;$i++) {
			$j = 0;
			foreach ($dataArr[$i-2] as $key=>$value) {
			    if($typeArr && $typeArr[$j] == 1)//数字
                    $phpsheet->setCellValue(IntToChr($j)."$i","$value");
			    else
				    $phpsheet->setCellValueExplicit(IntToChr($j)."$i","$value",\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
				if(strlen($value) > $widthArr[$j]){
					$widthArr[$j] = strlen($value);
					if($widthArr[$j] > 25) $widthArr[$j] = 25;
				}
				$j++;
			}
		}
		//设置列宽
		foreach($widthArr as $k=>$v){
			$phpsheet->getColumnDimension(IntToChr($k))->setWidth($v+2);
		}
		$phpwriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($phpexcel,"Xlsx");
		header('Content-Disposition: attachment;filename="'.date('YmdHis').'.xlsx"');
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		$phpwriter->save("php://output");
	}
	function import_excel($file='', $startrow=2){
		if(strpos(PRE_URL,$file)==0){
			$file = ROOT_PATH.ltrim(str_replace(PRE_URL,'',$file),'/');
		}
		$file = iconv("utf-8", "gb2312", $file);   //转码
		if(empty($file) OR !file_exists($file)) {
			echojson(['status'=>0,'msg'=>'file not exists!']);
		}
		$objRead = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');   //建立reader对象
		if(!$objRead->canRead($file)){
			$objRead = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xls');
			if(!$objRead->canRead($file)){
				echojson(['status'=>0,'msg'=>'No Excel!']);
			}
		}
		$PHPExcel = $objRead->load($file);
		$currentSheet = $PHPExcel->getSheet(0);  //读取excel文件中的第一个工作表
		$allColumn = $currentSheet->getHighestColumn(); //取得最大的列号
		$allRow = $currentSheet->getHighestRow(); //取得一共有多少行
		$exceldata = array();
		/**从第二行开始输出，因为excel表中第一行为列名*/
		for($currentRow = $startrow;$currentRow <= $allRow;$currentRow++){
			/**从第A列开始输出*/
			$erp_orders_id = array();  //声明数组
			for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){
				$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 64,$currentRow)->getValue();/**ord()将字符转为十进制数*/
				$erp_orders_id[] = $val;
			}
			$exceldata[] = $erp_orders_id;
		}
		return $exceldata;
	}

	public function inputlockpwd(){
		$lockpwd = input('param.lockpwd');
		$realpwd = Db::name('admin_set')->where('aid',aid)->value('locking_pwd');
		if($lockpwd != $realpwd){
			session('isunlock',0);
			return json(['status'=>0,'msg'=>'密码错误']);
		}else{
			session('isunlock',1);
			return json(['status'=>1,'msg'=>'操作成功']);
		}
	}
}
